//
//  SCBaseEmptyView.m
//  Shuaiche
//
//  Created by liyanan on 2019/6/17.
//  Copyright © 2019 zzs. All rights reserved.
//  空省页

#import "SCBaseEmptyView.h"
#import <Masonry.h>
#import "UIView+SCCreate.h"

@interface SCBaseEmptyView ()
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *msgLab;
@property (nonatomic, strong) UIButton *bottomBtn;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, strong) UILabel *titleLab;

@end
@implementation SCBaseEmptyView
- (instancetype)initWithFrame:(CGRect)frame showBottomBtn: (BOOL) showBtn {
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:UIColor.whiteColor];
        [self setUpSubViews: showBtn];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame imageName: (NSString *)imageName msg: (NSString *)msg showBottomBtn: (BOOL) showBtn btnTitle: (NSString *)btnTitle {
    self = [self initWithFrame:frame showBottomBtn:showBtn];
    if (self) {
        self.imageName = imageName;
        self.emptyTipString = msg;
        self.btntitle = btnTitle;
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title imageName: (NSString *)imageName msg: (NSString *)msg showBottomBtn: (BOOL)showBtn btnTitle: (NSString *)btnTitle {
    self.title = title;
    self = [self initWithFrame:frame imageName:imageName msg:msg showBottomBtn:showBtn btnTitle:btnTitle];
    if (self) {
        
    }
    return self;
}



- (void)bottomBtnAction: (UIButton *)sender {
    if ([_delegate respondsToSelector:@selector(didClickBottomBtn)]) {
        [_delegate didClickBottomBtn];
    }
}
- (void)setImageName:(NSString *)imageName {
    _imageName = imageName;
    [_imageView setImage:[UIImage imageNamed:_imageName]];
}
- (void)setEmptyTipString:(NSString *)emptyTipString {
    _emptyTipString = emptyTipString;
//    NSMutableString *str = [NSMutableString stringWithString:emptyTipString ?: @""];
//    [str replaceOccurrencesOfString:@"\n{2,}" withString:@"\n" options:NSRegularExpressionSearch range:NSMakeRange(0, str.length)];
    [_msgLab setText:emptyTipString];
}
- (void)setBtntitle:(NSString *)btntitle {
    _btntitle = btntitle;
    [_bottomBtn setTitle:btntitle forState:UIControlStateNormal];
}
#pragma mark- SubViews
- (void)setUpSubViews: (BOOL) showBtn {
    float topMargin = 113;
    if (kSCScreenHeight <= 667) {
        topMargin = 64;
    }
    float msgTopMargin = 15;
//    float btnTopMargin = 93;
    [self addSubview:self.imageView];
    [self addSubview:self.msgLab];
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self).offset(topMargin);
    }];
    if (self.title.length) {
        [self addSubview:self.titleLab];
        self.titleLab.text = self.title;
        msgTopMargin = 22;
//        btnTopMargin = 65;
        [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.imageView);
            make.top.equalTo(self.imageView.mas_bottom).offset(10);
        }];
    }
    [_msgLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_imageView);
        make.top.equalTo(self.title.length ? self.titleLab.mas_bottom : _imageView.mas_bottom).offset(msgTopMargin);
        make.left.equalTo(self).offset(15);
        make.right.equalTo(self).offset(-15);
    }];
    if (showBtn) {
        [self addSubview:self.bottomBtn];
        [_bottomBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_imageView);
            make.top.equalTo(_msgLab.mas_bottom).offset(25);
            make.height.equalTo(@45);
            make.width.equalTo(@140); 
        }];        
    }
}

- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        [_imageView setImage:[UIImage imageNamed:@"审核中"]];
    }
    return _imageView;
}

- (UILabel *)msgLab {
    if (!_msgLab) {
        _msgLab = [[UILabel alloc] init];
        _msgLab.textColor = SC_TextBlack_Secondary;
        _msgLab.font = SC_FONT(15);
        _msgLab.textAlignment = 1;
        [_msgLab setNumberOfLines:0];
    }
    return _msgLab;
}
- (UILabel *)titleLab {
    if (!_titleLab) {
         _titleLab = [[UILabel alloc] init];
        _titleLab.textColor = SC_TextBlack_Primary;
        _titleLab.font = SC_FONT_Meduim(15);
        _titleLab.textAlignment = 1;
//        [_titleLab setNumberOfLines:0];
    }
    return _titleLab;
}

-(UIButton *)bottomBtn {
    if (!_bottomBtn) {
        _bottomBtn = [UIView createMainTintButtonWithTitle:@"招募经纪人"];
        [_bottomBtn maskLayerWithCornerRadius:4];
        [_bottomBtn addTarget:self action:@selector(bottomBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _bottomBtn;
}

@end
